/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.selfmainten;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.bean.Condition;
import com.sinodata.bsm.center.bean.Page;
import com.sinodata.bsm.center.bean.WorkEnv;
import com.sinodata.bsm.center.cache.ResCache;
import com.sinodata.bsm.center.service.performance.PropValueService;
import com.sinodata.bsm.center.service.selfmainten.BsmService;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-9-24 上午9:47:46          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class BsmAction extends BaseAction {

    @Autowired
    private ResCache resCache;

    @Autowired
    private WorkEnv workEnv;

    @Autowired
    private PropValueService propService;
    @Autowired
    private BsmService bsmService;

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * 获取基本信息
     * @return
     */
    public String getBaseInfo() {
        JSONObject json = new JSONObject();
        String max = Runtime.getRuntime().maxMemory() / (1024 * 1024) + "";
        String total = Runtime.getRuntime().totalMemory() / (1024 * 1024) + "";
        String free = Runtime.getRuntime().freeMemory() / (1024 * 1024) + "";
        json.element("res", resCache.findAll().size() + "");
        json.element("max", max);
        json.element("total", total);
        json.element("free", free);
        json.element("usable", Integer.parseInt(max) - Integer.parseInt(total) + Integer.parseInt(free));
        json.element("jdkVer", System.getProperty("java.version"));
        json.element("javahome", System.getProperty("java.home"));
        json.element("osname", System.getProperty("os.name"));
        json.element("javavendor", System.getProperty("java.vendor"));
        json.element("osversion", System.getProperty("os.version"));
        json.element("username", System.getProperty("user.name"));

        Struts2Utils.renderText(json.toString());
        return null;
    }

    public String mq() {
        return "mq";
    }

    /**
     * 获取BSM配置信息
     * @return
     */
    public String getBSMInfo() {
        JSONObject json = new JSONObject();

        json.element("mq", workEnv.getActivemqUrl());
        json.element("uam", workEnv.getLoginUrl());

        json.element("notify", workEnv.getActivemqUrl());
        Struts2Utils.renderText(json.toString());
        return null;
    }

    /**
     * 查询指标采集数据  property_value_check
     * @return
     */
    public String queryCheckedProp() {
        JSONObject json = new JSONObject();
        JSONArray array = new JSONArray();
        Page<String[]> page = new Page<String[]>();
        Condition condition = new Condition();
        String resid = request.getParameter("resid");
        String ip = request.getParameter("ip");
        String propids = request.getParameter("propid");
        String status = request.getParameter("status");
        Map<String, String> map = new HashMap<String, String>();
        if (ip != null && ip.length() > 0) {
            map.put("ip", ip);
        }

        if (resid != null && resid.length() > 0) {
            map.put("resid", resid);
        }

        if (propids != null && propids.length() > 0) {
            map.put("propids", propids);
        }

        if (status != null && status.length() > 0) {
            map.put("status", status);
        }

        condition.setWhere(map);
        page.setPageNo(Integer.valueOf(request.getParameter("iDisplayStart")) / Integer.valueOf(request.getParameter("iDisplayLength")) + 1);
        page.setPageSize(Integer.valueOf(request.getParameter("iDisplayLength")));

        System.out.println("开始查询 ");
        page = propService.queryCheckedProp(page, condition);
        List<String[]> list = page.getRows();
        for (String[] stry : list) {
            JSONObject oj = new JSONObject();
            oj.element("resid", stry[1]);
            oj.element("name", stry[2]);
            oj.element("ip", stry[3]);
            oj.element("propid", stry[4]);
            oj.element("propname", stry[5]);
            oj.element("taskid", stry[6]);
            oj.element("cycle", stry[7]);
            oj.element("date", stry[8]);
            oj.element("value", stry[9]);

            array.add(oj);
        }

        json.element("iTotalRecords", page.getTotal());
        json.element("iTotalDisplayRecords", page.getTotal());
        json.element("aaData", array);
        Struts2Utils.renderText(json.toString());
        return null;
    }

    /**
     * 执行指标检查的存储过程
     * @return
     */
    public String doPropCheckProc() {
        JSONObject json = new JSONObject();
        String result = propService.doPropCheckProc();
        json.element("result", result);
        Struts2Utils.renderText(json.toString());
        return null;
    }

    /**
     * 跳转到BSM系统分析页面
     * @return
     */
    public String common() {
        return "common";
    }

    /**
     * 跳转到指标采集分析页面
     * @return
     */
    public String property() {
        String stacTime = propService.queryCheckedTime();
        if (stacTime.length() > 1) {
            stacTime = stacTime.substring(0, stacTime.length() - 2);
        }
        request.setAttribute("time", stacTime);
        return "property";
    }

    @Override
    public Object getModel() {
        return null;
    }

    @Override
    public String list() throws Exception {
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    public String save() throws Exception {
        return null;
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

    /**
     * 
    * @Title: table 
    * @Description: 跳转到bsm 数据库表展示页面
    * @return
    * @throws Exception
    * @author qiaoww 
    * @return String
    * @throws
     */
    public String table() throws Exception {

        return "table";
    }

    /**
     * 
    * @Title: queryTables 
    * @Description:查询bsm数据库 table列表
    * @throws Exception
    * @author qiaoww 
    * @return void
    * @throws
     */
    public void queryTables() throws Exception {
        List list = bsmService.querytables();
        JSONSerializer s = new JSONSerializer();
        //组装数据 返回
        JSONObject json = new JSONObject();
        json.element("iTotalRecords", list.size());
        json.element("iTotalDisplayRecords", list.size());
        json.element("aaData", s.toJSON(list));
        super.renderText(json.toString());

    }

    /**
     * 
    * @Title: procedure 
    * @Description: 跳转到bsm数据库存储过程展示页面
    * @return
    * @throws Exception
    * @author qiaoww 
    * @return String
    * @throws
     */
    public String procedure() throws Exception {
        return "procedure";
    }

    /**
     * 
    * @Title: trigger 
    * @Description: 跳转到bsm数据库job展示页面
    * @return
    * @throws Exception
    * @author qiaoww 
    * @return String
    * @throws
     */
    public String job() throws Exception {
        return "job";
    }

    /**
     * 
     * @Title: queryProcedure
     * @Description: 查询bsm数据库 存储过程列表
     * @return
     * @throws Exception
     * @author qiaoww
     * @return List
     * @throws
     */
    public void queryProcedure() throws Exception {
        List list = bsmService.queryProcedure();
        JSONSerializer s = new JSONSerializer();
        JSONArray array = new JSONArray();
        //组装数据 返回
        JSONObject json = new JSONObject();

        for (Object object : list) {
            JSONObject obj = new JSONObject();
            obj.put("OBJECT_NAME", ((Map) object).get("OBJECT_NAME"));
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            obj.put("STATUS", ((Map) object).get("STATUS"));
            obj.put("CREATED", df.format(((Map) object).get("CREATED")));
            obj.put("LAST_DDL_TIME", df.format(((Map) object).get("LAST_DDL_TIME")));
            obj.put("TIMESTAMP", ((Map) object).get("TIMESTAMP"));
            array.add(obj);
        }
        json.element("iTotalRecords", list.size());
        json.element("iTotalDisplayRecords", list.size());
        json.element("aaData", array);
        super.renderText(json.toString());
    }

    /**
     * 
    * @Title: queryJobs 
    * @Description: 查询bsm数据库job列表
    * @return
    * @throws Exception
    * @author qiaoww 
    * @return List
    * @throws
     */
    public void queryJobs() throws Exception {
        List list = bsmService.queryJobs();
        JSONSerializer s = new JSONSerializer();
        JSONArray array = new JSONArray();
        //组装数据 返回
        JSONObject json = new JSONObject();
        for (Object object : list) {
            JSONObject obj = new JSONObject();
            obj.put("WHAT", ((Map) object).get("WHAT"));
            obj.put("ID", ((Map) object).get("JOB"));
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            obj.put("BROKEN", ((Map) object).get("BROKEN"));
            if (((Map) object).get("LAST_DATE") != null) {
                obj.put("LAST_DATE", df.format(((Map) object).get("LAST_DATE")));
            }
            if (((Map) object).get("NEXT_DATE") != null) {
                obj.put("NEXT_DATE", df.format(((Map) object).get("NEXT_DATE")));
            }

            obj.put("INTERVAL", ((Map) object).get("INTERVAL"));
            obj.put("FAILURES", ((Map) object).get("FAILURES"));
            array.add(obj);
        }
        json.element("iTotalRecords", list.size());
        json.element("iTotalDisplayRecords", list.size());
        json.element("aaData", array.toString());
        super.renderText(json.toString());
    }

    public static void main(String[] args) {
        System.out.println("Java 运行时环境版本:" + System.getProperty("java.version"));
        System.out.println("Java 运行时环境供应商:" + System.getProperty("java.vendor"));

        System.out.println("Java 虚拟机规范版本:" + System.getProperty("java.vm.specification.version"));

        System.out.println("Java 类格式版本号:" + System.getProperty("java.class.version"));

        System.out.println("默认的临时文件路径:" + System.getProperty("java.io.tmpdir"));

        System.out.println("要使用的 JIT 编译器的名称:" + System.getProperty("java.compiler"));

        System.out.println("操作系统的名称:" + System.getProperty("os.name"));

        System.out.println("操作系统的架构:" + System.getProperty("os.arch"));

        System.out.println("操作系统的版本:" + System.getProperty("os.version"));

        System.out.println("用户的账户名称:" + System.getProperty("user.name"));

    }

}
